package com.yqg.manage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.manage.common.Result;
import com.yqg.manage.entity.SignTemplate;
import com.yqg.manage.service.ISignTemplateService;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;


/**
 * @Author: 小宇
 * @CreateTime: 2023/8/16 17:23
 */
@RestController
@RequestMapping("/signTemplate")
@Transactional(rollbackFor = Exception.class)
public class SignTemplateController {
    @Resource
    private ISignTemplateService signTemplateService;

    @ApiOperation("传文件file+account")
    @PostMapping("/save")
    public Result save(MultipartFile file,String account){
        SignTemplate signTemplate=new SignTemplate();
        String path = signTemplateService.uploadSign(file);
        signTemplate.setAccount(account);
        signTemplate.setFilePath(path);
        try {
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("account",account);
            long count = signTemplateService.count(queryWrapper);
            if (count==0) {//新增
                signTemplateService.save(signTemplate);
            }else {//编辑
                SignTemplate one = signTemplateService.getOne(queryWrapper);
                one.setFilePath(path);
                signTemplateService.updateById(one);
            }
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.success(false);
        }
        if (signTemplate == null) {
            return Result.error();
        }
        return Result.success(signTemplate);
    }

    @ApiOperation("传account")
    @GetMapping("/getSign")
    public Result getSign(@RequestParam String account){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("account",account);
        String theSign = signTemplateService.getOne(queryWrapper).getFilePath().toString();
        return Result.success(theSign);
    }
}
